package com.uton.rfidcontroller.view.base;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.components.support.RxFragment;
import com.uton.rfidcontroller.R;
import com.uton.rfidcontroller.common.utils.Utils;
import com.uton.rfidcontroller.viewmodel.base.BaseViewModel;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by 王家宝 on 2017/8/30.
 * Email:639586819@qq.com
 * Tel:18241150668
 */

public abstract class BaseFragment<VDB extends ViewDataBinding, VM extends BaseViewModel> extends RxFragment {

    protected VDB mBinding;

    protected VM mViewModel;

    protected Unbinder mBind;

    protected boolean isRefreshFirst;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, setLayout(), container, false);
        mViewModel = setViewModel();
        inject();
        View view = getBinding().getRoot();
        mBind = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //做其他事情
        doSomeThing();
    }

//    public abstract void refreshFirst();

    public abstract void doSomeThing();

    public abstract int setLayout();

    public abstract void inject();


    /**
     * 设置viewModel
     */
    public VDB getBinding() {
        return mBinding;
    }

    public VM getViewModel() {
        return mViewModel;
    }

    protected abstract VM setViewModel();

    /**
     * 添加线程调度
     */
    public <T> ObservableTransformer<T, T> compose(final LifecycleTransformer<T> lifecycle) {
        return new ObservableTransformer<T, T>() {
            @Override
            public ObservableSource<T> apply(Observable<T> observable) {
                return observable
                        .subscribeOn(Schedulers.io())
                        .doOnSubscribe(new Consumer<Disposable>() {
                            @Override
                            public void accept(Disposable disposable) throws Exception {
                                if (!Utils.isNetworkAvailable(getActivity())) {
                                    Utils.showShortToast(getString(R.string.toast_network_error));
                                }
                            }

                        })
                        .observeOn(AndroidSchedulers.mainThread())
                        .compose(lifecycle);
            }
        };
    }

    public void refreshFirst() {
        if (!isRefreshFirst) {
            isRefreshFirst = true;
            getViewModel().refreshData();
        }
    }

    public boolean isRefreshFirst() {
        return isRefreshFirst;
    }

    @Override
    public void onDestroy() {
        if (mBinding != null) {
            mBinding.unbind();
        }
        mBind.unbind();
        super.onDestroy();
    }
}
